package org.lisy.java.system;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class SystemOperate {

	public static void main(String[] args) {
		// 系统参数
		getSystemParam();
		// 判断 windows 操作系统
		isWindowsOS();
		// 获取 IP 地址
		getIpAdress();
	}

	private static void getSystemParam() {
        // 获取Java版本
        String javaVersion = System.getProperty("java.version");
        System.out.println("Java Version: " + javaVersion);
 
        // 获取Java运行时环境版本
        String javaRuntimeVersion = System.getProperty("java.runtime.version");
        System.out.println("Java Runtime Version: " + javaRuntimeVersion);
 
        // 获取操作系统名称
        String osName = System.getProperty("os.name");
        System.out.println("Operating System: " + osName);
 
        // 获取操作系统版本
        String osVersion = System.getProperty("os.version");
        System.out.println("Operating System Version: " + osVersion);
 
        // 获取用户的工作目录
        String userDir = System.getProperty("user.dir");
        System.out.println("User Directory: " + userDir);
 
        // 获取用户的账户名称
        String userName = System.getProperty("user.name");
        System.out.println("User Name: " + userName);
	}
	
	public static boolean isWindowsOS() {
        boolean isWindowsOS = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindowsOS = true;
            System.out.println("windows");
        } else {
        	System.out.println("other");
        }
        return isWindowsOS;
    }

	private static void getIpAdress() {
		// 使用 java.net.InetAddress 类的 getLocalHost() 方法
		try {
			InetAddress localHost = InetAddress.getLocalHost();
			String hostName = localHost.getHostName();
			String ipAddress = localHost.getHostAddress();
			System.out.println("主机名称：" + hostName + "，IP地址：" + ipAddress);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		// 使用 java.net.InetAddress 类的 getByName() 方法
		try {
			String hostName = "WINDOWS-UHECD8P";
			InetAddress inetAddress = InetAddress.getByName(hostName);
			String ipAddress = inetAddress.getHostAddress();
			System.out.println("IP地址：" + ipAddress);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		// 使用 java.net.NetworkInterface 类和 java.util.Enumeration 接口
		try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                    if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
                        System.out.println("主机名称：" + inetAddress.getHostName() + "，IP地址：" + inetAddress.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
	}

}
